﻿using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyFirstWPF;

namespace MyFirstWPF.View
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView : Window
    {
        public static int myId;
        public static string dbPath = "../../studyPlan.db";
        public static Window2 window2Instance;
        public LoginView()
        {
            InitializeComponent();
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void jump(object sender, RoutedEventArgs e)
        {
            using (var db = new StudyPlanDb(dbPath))
            {
                string name = tbUserName.Text;
                UserInfo user = db.Users.Where(x => x.Name == name).FirstOrDefault();               
                if (name == "")
                {
                    notice.Text = "未输入用户名";
                    notice.Foreground = new SolidColorBrush(Colors.Red);
                }
                else
                {
                    if (user == null)
                    {
                        UserInfo newUser = new UserInfo();
                        newUser.Name = name;
                        db.Insert(newUser);
                        notice.Foreground = new SolidColorBrush(Colors.Green);
                        notice.Text = "已为您新建账户,请重新登录";                      
                    }
                    else if(name == user.Name)
                    {
                        window2Instance = new Window2();
                        window2Instance.Show();   
                        this.Close();                                              
                        myId = user.Id;
                        notice.Text = "登录成功";
                    }
                }               
            }
           
        }
        private void WindowMove_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
            this.DragMove();
        }
    }
}

